package com.zzg.test2.web

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.leadbank.baselbf.log.LogBus
import com.zzg.test2.R
import com.zzg.test2.web.fragment.Tab2WebFragment

class WebMainActivity : AppCompatActivity() {
    private val TAG = "WebMainActivity"
    private lateinit var viewPager2: ViewPager2
    private lateinit var bottomNavigationView: BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_main)

        viewPager2 = findViewById(R.id.viewPager2)
        bottomNavigationView = findViewById(R.id.bottomNavigationView)

//        var tabWebJsFragment =
//            TabWebJsFragment.newInstance("https://www.leadfund.com.cn/html5/index/strict")

//        var web1: TabWebJsFragment? = null
//        var web2: TabWebJsFragment? = null
        viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = 5

            override fun createFragment(position: Int): Fragment {
                return when (position) {
                    0 ->{
                        LogBus.d(TAG, "createFragment  0")
                        WebHomeFragment()
                    }
//                    1 -> {
//                        Tab2WebFragment()
////                        web1 = TabWebJsFragment.newInstance("https://www.leadfund.com.cn/html5/index/strict")
////                        return web1!!
//                    }
//                    2 -> TabWebJsFragment.newInstance("https://www.leadfund.com.cn/html5/index/strict")
//                    2 -> {
//                       web2 =  TabWebJsFragment.newInstance("https://www.leadfund.com.cn/html5/index/strict")
//                        return web2!!
//                    }
                    1 -> {
                        LogBus.d(TAG, "createFragment  1")
//                        WebFragment.newInstance("https://www.leadfund.com.cn/html5/index/advicer")
                        WebFragment.newInstance("https://www.cmbc.com.cn/")
                    }
//                    2 -> NewsFragment()
                    2 ->{
                        LogBus.d(TAG, "createFragment  2")
                        //                        WebFragment.newInstance("https://www.cmbc.com.cn/")
//                        WebFragment.newInstance("https://www.leadfund.com.cn/html5/index/strict")
                        WebFragment.newInstance("https://www.iwencai.com/unifiedmobile/#/home/index")
                    }
//                    1 -> TabWebJsFragment.newInstance("https://www.iwencai.com/unifiedmobile/#/home/index")
//                    2 -> TabWebJsFragment.newInstance("https://www.iwencai.com/unifiedmobile/#/home/index")
//                    2 -> WebFragment.newInstance("https://www.cmbc.com.cn/")

//                    1 -> {
//                        jumpFragment1 = JumpFragment()
//                        return jumpFragment1!!
//                    }
//                    2 -> {
//                        jumpFragment2 = JumpFragment()
//                        return jumpFragment2!!
//                    }
                    3 -> {
                        LogBus.d(TAG, "createFragment  3")
                        NewsFragment()
                    }
                    4 ->{
                        LogBus.d(TAG, "createFragment  4")
                        MyFragment()
                    }
                    else -> {
                        MyFragment()
                    }
                }
            }
        }

        // 禁止 ViewPager2 滑动
        viewPager2.isUserInputEnabled = false

        // 保存 Fragment 状态
        viewPager2.offscreenPageLimit = 1

        bottomNavigationView.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.homeFragment -> viewPager2.currentItem = 0
                R.id.leadInvestFragment -> {
                    viewPager2.currentItem = 1
                }

                R.id.strictFragment -> {
                    viewPager2.currentItem = 2
                }

                R.id.newsFragment -> viewPager2.currentItem = 3
                R.id.myFragment -> viewPager2.currentItem = 4
            }
            true
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (viewPager2.currentItem != 0) {
                // 如果当前不在第一页，则返回到第一页
                viewPager2.currentItem = 0
                // 更新底部导航栏的选中状态
                bottomNavigationView.selectedItemId = R.id.homeFragment
            } else {
                // 如果已经在第一页，则执行默认的返回操作（退出应用）
//                super.onBackPressed()

                // 如果已经在第一页，则模拟按下Home键
                val homeIntent = Intent(Intent.ACTION_MAIN)
                homeIntent.addCategory(Intent.CATEGORY_HOME)
                homeIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(homeIntent)
            }

            return true
        }
        return false
    }
}